package com.hyb.controller;

import com.hyb.pojo.Items;
import com.hyb.pojo.User;
import com.hyb.service.ItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ItemsController {
    @Autowired
    ItemsService itemsService;
    @RequestMapping("/showitems")
    public String showitems(Map map, String t_id, String title, ModelMap maps){
        maps.put("t_id",t_id);
        maps.put("title",title);
        List<Items> itemList = itemsService.getAll(maps);
        map.put("itemList",itemList);
        return "items";
    }
    @RequestMapping("/toupdate")
        public String toupdate(@RequestParam int id,Map map){
            Items items=itemsService.getById(id);
            map.put("items",items);
            return "update";
    }
    @RequestMapping("/update")
    public String update(Items i,Map map){
        Date date1 = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String update_time = sf.format(date1);
        Items items = new Items();
        items.setEnd_time(i.getEnd_time());
        items.setUpdate_time(update_time);
        items.setId(i.getId());
        items.setPriority_id(i.getPriority_id());
        items.setItems_title(i.getItems_title());
        items.setItems_content(i.getItems_content());
        itemsService.update(items);
        map.put("t_id",1);
        return "redirect:/showitems";
    }
    @RequestMapping("/todelete")
    public String deleteItems(@RequestParam int id,Map map){
        itemsService.delete(id);
        map.put("t_id",1);
        return "redirect:/showitems";
    }
    @RequestMapping("/toadd")
    public String toadd(){
        return "add";
    }
    @RequestMapping("/additems")
    public String additems(HttpSession session,Items i,Map map){
        User user = (User)session.getAttribute("loginUser");
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String create_time = sf.format(date);
        Items items = new Items();
        items.setEnd_time(i.getEnd_time());
        items.setCreate_time(create_time);
        items.setU_id(user.getId());
        items.setItems_title(i.getItems_title());
        items.setItems_content(i.getItems_content());
        items.setPriority_id(i.getPriority_id());
        itemsService.add(items);
        map.put("t_id",1);
        return "redirect:showitems";
    }
    @Scheduled(cron = "0/5 * * * * ?")
    public String cc() {
        List<Items> itemslist = itemsService.quartzGetAll();
        Map<Object, Object> map = new HashMap<>();
        for (Items items : itemslist) {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                long time = new Date().getTime();
                long time1 = sf.parse(items.getCreate_time()).getTime();
                long end_time = (long)((items.getEnd_time())*3600000);
                if ((time - time1) >= end_time) {
                    itemsService.delete(items.getId());
                    System.out.println("11");
                    return "redirect:/showitems";
                }if((time - time1) < end_time && (time - time1) > (end_time - 300000)) {
                    map.put("color", "red");
                    return "redirect:/showitems";
                }
            }catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return "index";
    }
}
